import { message } from 'antd';
import { cloneDeep, isEmpty } from 'lodash-es';

import { LayerTypeBE } from '../components/Editor/components/TemplateEffect/constant';
import { getOpacityByData } from '../components/Editor/components/TemplateEffect/utils';
import { COPY_NAME_SUFFIX } from '../constant';
import {
  LayerInfoBE,
  LayerInfoFE,
  ProductAndTextLayerBody,
  TextLayerResponse
} from '../interface';

export const addRealTimePreviewParams = (params: {
  requestBody: ProductAndTextLayerBody;
}): ProductAndTextLayerBody => {
  const { requestBody } = params;

  const { layers } = requestBody;
  (layers as LayerInfoBE[]).forEach((layer) => {
    // text未填写内容时，显示默认文案
    if (layer.type === LayerTypeBE.Text && !layer.text?.content) {
      layer.text.content = 'TEXT';
      layer.text.fontColor = [155, 155, 155, 155];
      layer.text.fontFamily = 'Roboto-Italic,Roboto';
    }
  });

  return requestBody;
};

export const mergeRealTimePreviewData = (
  feLayers: LayerInfoFE[],
  realTimePreviewData: TextLayerResponse
): LayerInfoFE[] => {
  const newLayers = cloneDeep(feLayers);
  const { textLayers } = realTimePreviewData || {};

  textLayers.forEach((layer) => {
    const { id, textLayerImage, errorMessage } = layer;

    if (errorMessage) {
      message.warn(errorMessage);
    }

    const idLayer = newLayers.find((layer) => layer.data.id === id);

    idLayer.src = textLayerImage || ''; // 若请求失败无 textLayerImage，则不展示内容

    idLayer.data.needPreview = false;
    idLayer.opacity = getOpacityByData(idLayer.data);
  });

  return newLayers;
};

export const handleRefresh = (e: BeforeUnloadEvent): void => {
  e.returnValue = 'Are you sure you want to leave this page?';
};

export const mergeNewLayerIntoOldLayer = (
  newLayers: LayerInfoFE[],
  oldLayers: LayerInfoFE[]
): LayerInfoFE[] => {
  if (isEmpty(newLayers)) {
    return oldLayers;
  }

  return oldLayers.map((oldLayer) => {
    const newLayer = newLayers.filter(
      (newLayer) => newLayer.data.id === oldLayer.data.id
    );

    if (isEmpty(newLayer)) {
      return oldLayer;
    }

    return newLayer[0];
  });
};

export const hasEmptyContentTextLayer = (layers: LayerInfoFE[]): boolean =>
  layers.some((layer) => {
    if (layer.data.type === LayerTypeBE.Text && !layer.data.text?.content) {
      return true;
    }

    return false;
  });

export const hasEmptyImageLayer = (layers: LayerInfoFE[]): boolean =>
  layers.some((layer) => {
    if (layer.data.type === LayerTypeBE.Image && !layer.src) {
      return true;
    }

    return false;
  });

export const hasEmptyProductLayer = (layers: LayerInfoFE[]): boolean =>
  layers.some((layer) => {
    if (layer.data.type === LayerTypeBE.Product && !layer.src) {
      return true;
    }

    return false;
  });

export const hasEmptyOrAllHiddenProductImageLayer = (
  layers: LayerInfoFE[]
): boolean =>
  layers // 有 product image layer 没有上传图片，不可提交
    .some((layer) => {
      if (layer.data.type === LayerTypeBE.Product && !layer.src) {
        return true;
      }

      return false;
    }) ||
  layers // 或者所有 product image layer 都被隐藏，不可提交
    .filter((layer) => layer.data.type === LayerTypeBE.Product)
    .every((layer) => {
      if (layer.data.isHidden) {
        return true;
      }

      return false;
    });

export const generateCopyTemplateName = (oldName: string): string => {
  const newName = oldName.trim() + COPY_NAME_SUFFIX;
  return newName.length > 120 ? newName.substring(0, 120) : newName;
};
